<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>采购订单</title>
    <link rel="stylesheet" href="../../../public/dist/css/layui.css">
    <link rel="stylesheet" href="../../../css/purchaseorder.css">
    <link rel="stylesheet" href="../../../public/font/iconfont.css">
</head>

<body>
    <!-- 查询 -->
    <div class="select">
        <div class="select-left">
            <div class="select-item">
                <div class="select-item-text">
                    订单单号
                </div>
                <input type="text" class="layui-input select-item-input" id="a">
            </div>

            <div class="select-item">
                <div class="select-item-text">
                    订单状态
                </div>
                <select name="" id="b" class="select-item-select2 ">
                    <option value="">--请选择--</option>
                    <option value="10">采购订单提交待审核</option>
                    <option value="20">采购订单审核通过</option>
                    <option value="15">采购订单审核不通过</option>
                    <option value="30">采购订单待付款</option>
                    <option value="35">销售订单审核不通过</option>
                    <option value="40">采购订单付款完成</option>
                    <option value="50">销售订单发货</option>
                    <option value="60">采购订单收货</option>
                    <option value="70">采购订单申请退货</option>
                    <option value="80">采购方待退货</option>
                    <option value="85">销售方不同意退货</option>
                    <option value="90">采购退货</option>
                    <option value="100">销售收获并退款</option>

                </select>
            </div>

        </div>

        <div class="select-right">
            <div class="select-btn   search" lay-event="search">
                查询
            </div>
            <div class="select-btn    reset" lay-event="reset">
                清除
            </div>
        </div>
    </div>

    <div class="common-padding">
        <table class="layui-hide" id="testdome" lay-filter="testdome"></table>
    </div>
</body>
<script src="../../../public/dist/layui.js" charset="utf-8"></script>
<script src="../../../js/common/jquery.min.js"></script>
<script src="../../../js/common/api.js"></script>

</html>

<!-- 头部 -->
<script type="text/html" id="toolbarinter">
    <div class="layui-btn-container ">
        <button class="layui-btn  layui-btn-sm colors layui-bg-black" lay-event="add"><i class="layui-icon"></i></i>添加</button>
        <button class="layui-btn  layui-btn-sm colors  layui-bg-black" lay-event="update"><i class="layui-icon"></i>编辑</button>
        <button class="layui-btn  layui-btn-sm colors  layui-bg-black" lay-event="delete"><i class="layui-icon"></i>删除</button>
</script>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>
</script>


<script>
    layui.use(["table", "laydate", "layer", "util"], function () {
        var $ = layui.jquery,
            util = layui.util,
            laydate = layui.laydate,
            layer = layui.layer,
            table = layui.table;



        //监听头部监听
        table.on('toolbar(testdome)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id),
                data = checkStatus.data; //获取选中的数据
            console.log(data)
            switch (obj.event) {
                case 'add':
                    window.location.href = "./dialog/addpurchaseorder.html";
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        window.location.href = "./dialog/addpurchaseorder.html?id=" + data[0].id;
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        noAjax("coa/delOrder", {
                            id: data[0].id
                        }, "POST").done(function (resp) {
                            console.log(resp)
                            console.log(data[0].id)
                            layer.msg('操作成功', {
                                icon: 1,
                                time: 1000
                            });
                            setTimeout(function () {
                                tableIns.reload({
                                    where: {},
                                    page: {
                                        curr: 1, //重新从第 1 页开始
                                        layout: ['prev', 'page', 'next', 'skip', 'count']
                                    },
                                })
                            }, 1500)
                            return
                        }).fail(function (err) {
                            console.log(err)
                        });
                    }
                    break;
            };
        });


        var tableIns = table.render({
            elem: "#testdome",
            url: base + "coa/getOrderList",
            method: "GET",
            where: {
                transType: "采购订单"
            },
            headers: {
                Authorization: "Bearer" + " " + sessions
            },
            request: {
                pageName: "currentPage",
                limitName: "pageSize"
            },
            toolbar: "#toolbarinter",
            limits: [10, 20],
            parseData: function (res) {
                //res 即为原始返回的数据
                console.log(res)
                return {
                    code: res.code, //解析接口状态
                    msg: res.message, //解析提示文本
                    totalNum: res.pageBean.totalNum, //解析数据长度
                    lists: res.pageBean.lists //解析数据列表
                };
            },
            response: {
                statusName: "code", //数据状态的字段名称，默认：code
                statusCode: 10000, //成功的状态码，默认：0
                msgName: "message", //状态信息的字段名称，默认：msg
                countName: "totalNum", //数据总数的字段名称，默认：count
                dataName: "lists" //数据列表的字段名称，默认：data
            },

            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            page: {
                //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ["prev", "page", "next", "skip", "count"], //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                groups: 5, //只显示 1 个连续页码
                first: true, //不显示首页
                last: true, //不显示尾页
                prev: "上一页",
                next: "下一页",
                theme: "#c81623"
            },
            // height: 'full-20',//满高
            cols: [
                [{
                    type: 'checkbox',
                    fixed: "left",
                }, {
                    field: "transNo",
                    title: "采购单号",
                    minWidth: 110,
                    align: "center"
                }, {
                    field: "transDate",
                    title: "采购日期",
                    minWidth: 120,
                    sort: true,
                    align: "center",
                    templet: function (d) {
                        var numv = null;
                        if (d.transDate == "" || d.transDate == null) {
                            numv = " ";
                        } else {
                            numv = d.transDate = layui.util.toDateString(
                                d.transDate,
                                "yyyy-MM-dd"
                            );
                        }
                        return numv;
                    }
                },{
                    field: "transStatus",
                    title: "订单状态",
                    minWidth: 200,
                    sort: true,
                    align: "center",
                    templet: function (d) {
                        var numv = null;
                        if (d.transStatus == "10") {
                            numv = "采购订单提交待审核";
                        }
                        else if (d.transStatus == "20") {
                            numv = "采购订单审核通过";
                        }
                        else if (d.transStatus == "15") {
                            numv = "采购订单审核不通过";
                        }
                        else if (d.transStatus == "30") {
                            numv = "采购订单待付款";
                        }
                        else if (d.transStatus == "35") {
                            numv = "销售订单审核不通过";
                        }
                        else if (d.transStatus == "40") {
                            numv = "采购订单付款完成";
                        }
                        else if (d.transStatus == "50") {
                            numv = "销售订单发货";
                        }
                        else if (d.transStatus == "60") {
                            numv = "采购订单收货";
                        }
                        else if (d.transStatus == "70") {
                            numv = "采购订单申请退货";
                        }
                        else if (d.transStatus == "80") {
                            numv = "采购方待退货";
                        }
                        else if (d.transStatus == "85") {
                            numv = "销售方不同意退货";
                        }
                        else if (d.transStatus == "90") {
                            numv = "采购退货";
                        }
                        else if (d.transStatus == "100") {
                            numv = "销售收获并退款";
                        }
                        return numv;
                    }
                }, {
                    field: "customerVendorName",
                    title: "供应商名称",
                    minWidth: 100,
                    sort: true,
                    align: "center"
                }, {
                    field: "salesman",
                    title: "采购员名称",
                    minWidth: 100,
                    sort: true,
                    align: "center"
                }, {
                    field: "estShipDate	",
                    title: "预交日",
                    minWidth: 120,
                    sort: true,
                    align: "center",
                    templet: function (d) {
                        var numv = null;
                        if (d.estShipDate == "" || d.estShipDate == null) {
                            numv = " ";
                        } else {
                            numv = d.estShipDate = layui.util.toDateString(
                                d.estShipDate,
                                "yyyy-MM-dd"
                            );
                        }
                        return numv;
                    }
                }, {
                    field: "remark",
                    title: "备注",
                    minWidth: 100,
                    align: "center"
                }, {
                    field: "userName",
                    title: "制表人名称",
                    minWidth: 100,
                    align: "center"
                }, {
                    field: "editTime",
                    title: "单身数据",
                    minWidth: 80,
                    align: "center",
                    toolbar: '#barDemo'
                }, {
                    field: "editTime",
                    title: "修改时间",
                    minWidth: 130,
                    sort: true,
                    align: "center",
                    fixed: 'right',
                    templet: function (d) {
                        var numv = null;
                        if (d.editTime == "" || d.editTime == null) {
                            numv = " ";
                        } else {
                            numv = d.editTime = layui.util.toDateString(
                                d.editTime,
                                "yyyy-MM-dd"
                            );
                        }
                        return numv;
                    }
                },
                ]
            ]
        });

        /*查询*/
        $(".search").click(function () {
            if ($("#a").val() == "" && $("#b").val() == "") {
                layer.msg("请输入查询条件！", {
                    time: 1000,
                    offset: "t"
                });
                return;
            }

            var datelist = {
                transNo: $("#a").val(),
                transStatus: $("#b").val(),
                transType: "采购订单"
            };

            tableIns.reload({
                where: datelist,
                request: {
                    pageName: "currentPage", //页码的参数名称，默认：page
                    limitName: "pageSize" //每页数据量的参数名，默认：limit
                },
                page: {
                    curr: 1, //重新从第 1 页开始
                    layout: ["prev", "page", "next", "skip", "count"]
                }
            });
        });

        /*重置*/
        $(".reset").click(function () {
            $("#a").val("");
            $("#b").val("");
            tableIns.reload({
                where: {
                    transType: "采购订单"
                },
                page: {
                    curr: 1, //重新从第 1 页开始
                    layout: ["prev", "page", "next", "skip", "count"]
                }
            });
        });



        //查看
        var rys = null;
        table.on('tool(testdome)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'edit') { //查看
                layer.open({
                    type: 1,
                    title: "单身数据",
                    shadeClose: true, //是否点击遮罩关闭
                    anim: 5, //弹出动画
                    scrollbar: false, //窗口外滚动条是否出现
                    skin: 'layui-layer-rim', //加上边框
                    area: ['860px', '300px'], //宽高
                    content: '<div id="sssd"  style="margin:10px;"></div>',
                    success: function (layero, index) {
                        console.log(data.coOrderSingle)
                        $.each(data.coOrderSingle, function (v, its) {
                            rys = '<tr>' +
                                '<td>' + its.partDescription + '</td>' +
                                '<td>' + its.partId + '</td>' +
                                '<td>' + its.partSpecification + '</td>' +
                                '<td>' + its.quantity + '</td>' +
                                '<td>' + its.unit + '</td>' +
                                '<td>' + its.taxRate + '</td>' +
                                '<td>' + its.taxPrice + '</td>' +
                                '<td>' + its.amount + '</td>' +
                                '<td>' + its.allAmt + '</td>' +
                                '<td>' + its.zkRate + '</td>' +
                                '<td>' + its.locationName + '</td>' +
                                '<td>' + its.remark + '</td>' +
                                '<td>' + its.perQty + '</td>' +
                                '</tr>'
                        })
                        console.log(rys)

                        $("#sssd").html('<table lay-filter="parse-table-demo">' +
                            '<thead> <tr>' +
                            '<th lay-data="{field:\'username\', minWidth:100}">物料名称</th>' +
                            '<th lay-data="{field:\'d\', minWidth:100}">材料代号</th>' +
                            '<th lay-data="{field:\'userwname\', minWidth:100}">物料规格</th>' +
                            '<th lay-data="{field:\'w\', minWidth:100}">采购数量</th>' +
                            '<th lay-data="{field:\'e\', minWidth:100}">单位</th>' +
                            '<th lay-data="{field:\'r\', minWidth:100}">税率</th>' +
                            '<th lay-data="{field:\'t\', minWidth:100}">含税单价</th>' +
                            '<th lay-data="{field:\'y\', minWidth:100}">采购金额</th>' +
                            '<th lay-data="{field:\'u\', minWidth:100}">价税合计</th>' +
                            '<th lay-data="{field:\'i\', minWidth:100}">折扣率</th>' +
                            '<th lay-data="{field:\'o\', minWidth:100}">交货仓库</th>' +
                            '<th lay-data="{field:\'p\', minWidth:100}">备注</th>' +
                            '<th lay-data="{field:\'q\', minWidth:100}">装箱数量/箱</th>' +
                            '</tr> ' +
                            '</thead>' +
                            '<tbody>' + rys + '</tbody>' +
                            '</table> ')
                        table.init('parse-table-demo', { //转化静态表格
                        });
                    }
                });
            }
        });
    });
</script>